/*
Copyright 2011 Peter Krajnc, SCREENWORKER

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.screenworker.compactmvc.utils
{
	import flash.display.InteractiveObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.utils.getDefinitionByName;
	
	/**
	 * Frame Two loader class
	 * 
	 * necessary compiler arguments:	
	 * -frame two [classpath to the ApplicationMain Class]
	 * 
	 */
	
	public class FrameTwoLoader extends MovieClip
	{
		protected var _progress:Number = 0;
		protected var _isComplete:Boolean;
		protected var _launcherClassPath:String;
		protected var _launcherClass:Class;
		protected var _mainApplicationClassPath:String;
		protected var _target:MovieClip;
		
		public function FrameTwoLoader(launcherClassPath:String)
		{
			super();
			_launcherClassPath = launcherClassPath;
			_target = this;
		}
		
		/**
		 * starts loading 
		 * 
		 */        
		public function startLoading():void
		{
			_target.addEventListener(Event.ENTER_FRAME, enterFrameHandler, false, 0, true);
		}
		
		/**
		 * stops loading 
		 * 
		 */        
		public function stopLoading():void
		{
			_target.removeEventListener(Event.ENTER_FRAME, enterFrameHandler); 
		}
		
		/**
		 * loop event  
		 * @param event
		 * 
		 */		
		protected function enterFrameHandler(event:Event):void 
		{
			progress = _target.root.loaderInfo.bytesLoaded / _target.root.loaderInfo.bytesTotal;
			if(_target.framesLoaded >= 2) 
			{   
				_progress = 1;
				
				//jump in to frame two
				if(_target.currentFrame < 2)
					_target.nextFrame();  
				
				//try to access main class in frame two
				if(isMainClassAccessible())
				{
					_target.removeEventListener(Event.ENTER_FRAME, enterFrameHandler); 
					_isComplete = true;
					onComplete();
				}                    
			}          
		}
		
		protected function onComplete():void
		{
			var mainApp:InteractiveObject = new _launcherClass();
			addChild(mainApp);
		}
		
		public function isMainClassAccessible():Boolean
		{
			try
			{
				_launcherClass = getDefinitionByName(_launcherClassPath) as Class;
				return true;       
			}
			catch(e:Error)
			{
				trace("FrameTwoLauncher->isMainClassAccessible->Error:" + e);
				return false;
			}
			
			return false;
		}
		
		public function get isComplete():Boolean
		{
			return _isComplete;
		}
		
		/**
		 * Returns loading progress
		 * @return 
		 * 
		 */
		public function get progress():Number
		{
			return _progress;
		}
		
		public function set progress(value:Number):void
		{
			_progress = value;
		}
		
		
	}
}